package controller

import (
	"api/app/auth/auto/v1/api"
	"api/app/auth/models"
	"api/app/auth/service"
	"gitee.com/dreamwood/ez/server"
)

type UserController struct {
	api.UserApi
}

func init() {

	c := &UserController{}
	c.Prepare()
	c.AddRoute("/list", c.ListAction)
	c.AddRoute("/get", c.GetAction)
	c.AddRoute("/scan", c.ScanAction)
	c.AddRoute("/choice", c.ChoiceAction)
	c.AddRoute("/save", c.SaveAction)
	c.AddRoute(`/save_many`, c.SaveManyAction)
	c.AddRoute("/delete", c.DeleteAction)
	c.AddRoute("/delete_many", c.DeleteManyAction)

	c.AddRoute("/reset_password", c.ResetPasswordAction)

}

//如果不满意自动生成的方法，请自定义，本文件不会被生成器覆盖，请放心操作
//func (this *UserController) SaveAction(operation *server.Operation) {}
//func (this *UserController) GetAction(operation *server.Operation) {}
//func (this *UserController) ScanAction(operation *server.Operation) {}
//func (this *UserController) ListAction(operation *server.Operation) {}
//func (this *UserController) DeleteAction(operation *server.Operation) {}

func (c *UserController) ResetPasswordAction(operation *server.Operation) {
	this := c.TakeOver(operation)
	id := this.Get("id").IsInt()
	crud := models.NewUserCrud()
	user, e := crud.Find(id)
	if this.Try(e) {
		return
	}
	user.Password = service.GenPass("123123", user.Salt)
	user.Save()
	this.ReturnSuccess("密码已重置为：123123", "")
}
